package com.adwin.addcoin.ui.seetting

import android.app.Activity
import android.app.Application
import android.widget.Toast
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.adwin.addcoin.repository.SchedulerTransformer
import com.adwin.addcoin.repository.api.TestApi
import com.adwin.addcoin.repository.net.RetrofitClient

class SettingModel(application: Application): AndroidViewModel(application) {
    var isLogin = MutableLiveData<Boolean>(true)
    fun logOut(activity: Activity){
        RetrofitClient.getInstance().getService(TestApi::class.java).logOut().compose(
            SchedulerTransformer()
        ).subscribe({
            if (it.isSuccessful){
                if (it.body()?.code == 200&&it.body()?.data == true){
                    isLogin.value = false
                    Toast.makeText(activity,it.body()?.msg?:"",Toast.LENGTH_SHORT).show()
                }else if (it.body()?.code == 200){
                    Toast.makeText(activity,it.body()?.msg?:"",Toast.LENGTH_SHORT).show()
                }else{
                    Toast.makeText(activity,it.message()?:"",Toast.LENGTH_SHORT).show()
                }

            }
        },{
            Toast.makeText(activity,it.message?:"",Toast.LENGTH_SHORT).show()
        })
    }

    fun userDelete(activity: Activity){
        RetrofitClient.getInstance().getService(TestApi::class.java).userDelete().compose(
            SchedulerTransformer()
        ).subscribe({
            if (it.isSuccessful){
                if (it.body()?.code == 200&&it.body()?.data == true){
                    isLogin.value = false
                    Toast.makeText(activity,it.body()?.msg?:"",Toast.LENGTH_SHORT).show()
                }else if (it.body()?.code == 200){
                    Toast.makeText(activity,it.body()?.msg?:"",Toast.LENGTH_SHORT).show()
                }else{
                    Toast.makeText(activity,it.message()?:"",Toast.LENGTH_SHORT).show()
                }

            }
        },{
            Toast.makeText(activity,it.message?:"",Toast.LENGTH_SHORT).show()
        })
    }
}